package com.hoki.zj.hrm;

import com.hoki.zj.hrm.domain.KillCourse;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.hoki.zj.hrm.service.impl.KillCourseServiceImpl.REDIS_PUBLISH_KEY;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class testGetKillCourseInRedis {

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Autowired
    private RedissonClient redissonClient ;

    @Test
    public void test1() throws Exception {
        // 1.获取Redis中发布的课程
        BoundHashOperations<Object, String, KillCourse> ops = redisTemplate.boundHashOps(REDIS_PUBLISH_KEY);
        List<KillCourse> killCourses = ops.values();
        Date now = new Date();
        // 2.过滤发布课程中所有未过期的课程并返回
        assert killCourses != null;
        List<KillCourse> collect = killCourses.stream().
                filter(killCourse -> killCourse.getEndTime().after(now)).
                collect(Collectors.toList());

        collect.forEach(System.out :: println);
    }

    @Test
    public void test2() throws Exception {
        // 1.获取Redis中发布的课程
        BoundHashOperations<Object, String, KillCourse> ops = redisTemplate.boundHashOps(REDIS_PUBLISH_KEY);
        List<KillCourse> killCourses = ops.values();
        Date now = new Date();
        // 2.过滤当前id,并且未过期的课程
        assert killCourses != null;
        List<KillCourse> collects = killCourses.stream()
                .filter(killCourse -> killCourse.getId().equals(6L) && killCourse.getEndTime().after(now))
                .collect(Collectors.toList());
        System.out.println(collects.isEmpty());
        System.out.println(collects.get(0));
    }
}
